<?php

namespace App\Kuafu\Guanyuan\Command;

use App\Kuafu\Common\Service\CommonService;
use App\Kuafu\Config\Model\ConfigModel;
use App\Kuafu\Shop\Model\Shop;
use Illuminate\Console\Command;

class SetConfigCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'guanyuan:setShopConfig';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '观远订单同步:设置需要同步的店铺信息';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command
     */
    public function handle()
    {

        $inShopList = [
            19 => [
                'name'      => '古美路店',
                'startTime' => '2021-01-01'
            ],
            20 => [
                'name'      => '三钢里',
                'startTime' => '2021-01-01'
            ],
            21 => [
                'name'      => '丽都店',
                'startTime' => '2021-01-01'
            ],
            28 => [
                'name'      => '苏州狮山龙湖天街店',
                'startTime' => '2021-12-17'
            ]
        ];

        $configType = 'guanyuanPushShop';
        ConfigModel::delDataByType($configType);

        // 获取店铺信息

        $shopList = Shop::query()
                        ->whereIn('id', array_keys($inShopList))
                        ->get()
                        ->each(function ($item) {
                            if (empty($item->province)) {
                                dd('id:' . $item->id . ', ' . $item->name . '的省份(province)为空');
                            }
                            if (empty($item->city)) {
                                dd('id:' . $item->id . ', ' . $item->name . '的市(city)为空');
                            }
                            if (empty($item->district)) {
                                dd('id:' . $item->id . ', ' . $item->name . '的区(district)为空');
                            }
                        })
                        ->each(function ($item) use ($configType, $inShopList) {
                            $config        = new ConfigModel();
                            $config->name  = CommonService::zeroFill($item->shopCode) . $item->name;
                            $config->key   = $item->id;
                            $config->value = json_encode(
                                [
                                    'startTime' => $inShopList[$item->id]['startTime'],
                                ]
                            );
                            $config->type  = $configType;
                            $config->save();
                        });


    }
}
